package com.iplant.wms.serviceImpl.dao.brm;

import com.iplant.base.po.OutResult;
import com.iplant.base.po.Pagination;
import com.iplant.base.po.enums.ExceptionTypes;
import com.iplant.base.utils.annotation.dao.DBBaseDAO;
import com.iplant.base.utils.sql.BaseDAO;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.wms.service.po.brm.BRMShelvesRule;
import lombok.extern.slf4j.Slf4j;

import java.util.*;

@Slf4j
@DBBaseDAO(BRMShelvesRule.class)
public class BRMShelvesRuleDAO extends BaseDAO {

    private static BRMShelvesRuleDAO Instance = null;

    private BRMShelvesRuleDAO() {
        super("");
        this.Init();
    }

    public static BRMShelvesRuleDAO getInstance() {
        if (Instance == null) {
            Instance = new BRMShelvesRuleDAO();
        }
        return Instance;
    }



    public void BRM_DeleteShelvesRule(BMSEmployee wLoginUser, BRMShelvesRule wShelvesRule, OutResult<Integer> wErrorCode) {
        wErrorCode.set(0);

        try {
            if (wShelvesRule == null || wShelvesRule.ID <= 0) {
                wErrorCode.set(ExceptionTypes.Parameter.getValue());
                return;
            }
            int wRows = this.DeletePO(wLoginUser, wShelvesRule, wErrorCode);

            if (wRows > 0) {
                BRMShelvesPropDAO.getInstance().BRM_DeleteShelvesProp(wLoginUser, wShelvesRule.ID, wErrorCode);
                BRMShelvesRangeDAO.getInstance().BRM_DeleteShelvesRange(wLoginUser, wShelvesRule.ID, wErrorCode);

                BRMReplenishRuleDAO.getInstance().BRM_DeleteReplenishRule(wLoginUser, wShelvesRule.ID, wErrorCode);
            }

        } catch (Exception ex) {
            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            log.error("BRM_UpdateShelvesRule", ex);
        }
    }


    public List<BRMShelvesRule> BRM_QueryShelvesRuleList(
            BMSEmployee wLoginUser, String wName, int wStoreID,
            int wActive, Pagination wPagination, OutResult<Integer> wErrorCode) {

        Map<String, Object> wParams = new HashMap<>();

        wParams.put("Name", wName);
        wParams.put("StoreID", wStoreID);
        wParams.put("Active", wActive);

        return this.SelectPOList(wLoginUser, wParams, wPagination, wErrorCode);
    }

}
